package org.jz.gae.redbook.spb.client.slate.district;

import java.util.ArrayList;
import java.util.List;

import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid;
import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid.ColumnValueDisplayer;
import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.DistrictEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class DistrictListPlate extends Composite
{
	  private final Button refreshButton = new Button("Обновить");
	  private final Button addButton = new Button("Добавить");
	  private final Button openButton = new Button("Открыть");
	  private final FlexTable contentTable;
	  private EntityViewGrid<DistrictEntityView> districtListGrid;
	  private Slate slate; 
	  
	  public DistrictListPlate() 
	  {
		  SimplePanel contentTableDecorator = new SimplePanel();
	    initWidget(contentTableDecorator);
	    contentTableDecorator.setWidth("100%");

	    contentTable = new FlexTable();
	    contentTable.setWidth("100%");
	    contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListContainer");
	    contentTable.getCellFormatter().setWidth(0, 0, "100%");
	    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);
	    
	    // Create the menu
	    //
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setBorderWidth(0);
	    hPanel.setSpacing(0);
	    hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
	    hPanel.add(addButton);
	    contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListMenu");
	    contentTable.setWidget(0, 0, hPanel);
	    
	    // Create the contacts list
	    //
	    districtListGrid = new EntityViewGrid<DistrictEntityView>();
	    districtListGrid.addButton(refreshButton);
	    districtListGrid.addButton(addButton);
	    districtListGrid.addButton(openButton);
	    
	    districtListGrid.addGridColumn("Наименование", "280px", new ColumnValueDisplayer<DistrictEntityView>(){
		    	@Override
		    	public String displayColumnValue(DistrictEntityView _EntityView)
		    	{
		    		return _EntityView.getDistrictRusName();
		    	}
		    },
		    new TextColumn<DistrictEntityView>(){
		    	@Override
		    	public String getValue(DistrictEntityView _EntityView)
		    	{
		    		return _EntityView.getDistrictRusName();
		    	}
		    });
	    districtListGrid.addGridColumn("Name", null, new ColumnValueDisplayer<DistrictEntityView>(){
		    	@Override
		    	public String displayColumnValue(DistrictEntityView _EntityView)
		    	{
		    		return _EntityView.getDistrictEngName();
		    	}
		    },
		    new TextColumn<DistrictEntityView>(){
		    	@Override
		    	public String getValue(DistrictEntityView _EntityView)
		    	{
		    		return _EntityView.getDistrictEngName();
		    	}
		    });
	    contentTable.setWidget(1, 0, districtListGrid);
	    
	    contentTableDecorator.add(contentTable);
	    
	    refreshButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	fetchDistrictDetails();
	        }
	      });
	    
	    addButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	appendNewDistrict();
 	        }
	      });

	    openButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	openSelectedDistrict();
	        }
	      });
	  }
	  
	  public void showDistrictList(
			  Slate _Slate)
	  {
		 slate = _Slate;
		 fetchDistrictDetails();  
	  }
	  
	  private void fetchDistrictDetails() 
	  {
		    slate.getContainerAgent().getRpcService().getDistrictDetails(new AsyncCallback<ArrayList<DistrictEntityView>>() {
		      public void onSuccess(ArrayList<DistrictEntityView> result) 
		      {
		    	  List<DistrictEntityView> districtDetails = result;
		          //sortDistrictDetails(districtDetails);
		          districtListGrid.setData(districtDetails);
		      }
		      
		      public void onFailure(Throwable caught) {
		        Window.alert("Error fetching district details");
		      }
		    });
		  }

      private void appendNewDistrict()
      {
          DistrictAppendPlate appendPlate = new DistrictAppendPlate();
          appendPlate.appendEntityView(slate, new ClickHandler(){
             	  @Override
		        public void onClick(ClickEvent event)
		        {
		        	fetchDistrictDetails();
		        }
          });
      }
	  
      private void openSelectedDistrict()
      {
      	DistrictEntityView district = districtListGrid.getSelectedEntityView();
      	if (district != null)
      	{
              String id = district.getDistrictKey();
              /**/
              //DistrictDetailsPlate detailsPlate = new DistrictDetailsPlate();
              //detailsPlate.openEntityView(slate, id);
      	}
      	else
      	{
              System.err.println("event releasing : no district chosen");  
      	}
      }
	  
      private void deleteSelectedDistrict()
      {
    	  
      }
	  
}
